﻿using FluentValidation;
using Sgr.DataCategories.Application.Commands.AdministrativeDivisions;

namespace Sgr.DataCategories.Application.Validations.AdministrativeDivisions
{
    /// <summary>
    /// 创建行政区划命令验证器
    /// </summary>
    public class CreateAdministrativeDivisionCommandValidator
        : AbstractValidator<CreateAdministrativeDivisionCommand>
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public CreateAdministrativeDivisionCommandValidator()
        {
            RuleFor(x => x.Code)
                .NotEmpty().WithMessage("行政区划代码不能为空")
                .MaximumLength(50).WithMessage("行政区划代码长度不能超过50个字符");

            RuleFor(x => x.Name)
                .NotEmpty().WithMessage("行政区划名称不能为空")
                .MaximumLength(100).WithMessage("行政区划名称长度不能超过100个字符");

            RuleFor(x => x.ShortName)
                .MaximumLength(50).WithMessage("行政区划简称长度不能超过50个字符")
                .When(x => !string.IsNullOrEmpty(x.ShortName));

            RuleFor(x => x.Description)
                .MaximumLength(500).WithMessage("行政区划描述长度不能超过500个字符")
                .When(x => !string.IsNullOrEmpty(x.Description));

            RuleFor(x => x.ParentId)
                .GreaterThanOrEqualTo(0).WithMessage("上级行政区划ID必须大于或等于0")
                .When(x => x.ParentId.HasValue);
        }
    }
}